import { ObjCreate } from "../../common/ObjCreate";
import { GImageBitmap } from "../../../../../com/mui/controls/GImageBitmap";
import { ImagesConst } from "../../../resource/ImagesConst";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { GTextFormat } from "../../../../common/GTextFormat";
import { Language } from "../../../../../extend/language/Language";
import { PropItem } from "./PropItem";
import { EquipPropButton } from "../../equipment/deploy/EquipPropButton";
import { AcupointRecommendPanel } from "./AcupointRecommendPanel";
import { ItemConfig } from "../../../resource/ItemConfig";
import { ECategory } from "../../../../../Message/Public/ECategory";
import { EProp } from "../../../../../Message/Public/EProp";
import { ItemData } from "../../../resource/info/ItemData";
import { AcupointData } from "./AcupointData";
import { AcupointConfig } from "../../../resource/AcupointConfig";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { MsgManager } from "../../../manager/MsgManager";
import { QuickBuyWindow } from "../../common/QuickBuyWindow";
import { Cache } from "../../../cache/Cache";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { SAttributeUpdate } from "../../../../../Message/Public/SAttributeUpdate";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { ItemInfo } from "../../../resource/info/ItemInfo";
import { GTextFiled } from "../../../../../com/mui/controls/GTextFiled";
import { _step } from "../../../../../this/_step";
type int = number;
//class RootBreakPanel
    
    export  class RootBreakPanel /* flash.display.Sprite */
    {
        constructor()
        {
            this._pointArr = [{"x": 120, "y": 85}, {"x": 70, "y": 122}, {"x": 170, "y": 122}, {"x": 85, "y": 185}, {"x": 155, "y": 185}];
            this._propItemArr = [];
            this._luckPropArr = [];
            
            this.init();
            return;
        }

        /* internal  */init(): void
        {
            var loc3=null;
            var loc4=null;
            ObjCreate.createBg(0, 0, 279, 395, this);
            addChild(new GImageBitmap(ImagesConst.WuXingBg, 25, 50));
            ObjCreate.createBg(5, 55, 264, 2, this, ImagesConst.SplitLine);
            ObjCreate.createBg(5, 260, 264, 2, this, ImagesConst.SplitLine);
            this._rootTxt = ObjCreate.createGTextField("", 20, 19, 230, 20, this, GlobalStyle.textFormatOrange);
            this._addDesTxt = ObjCreate.createGTextField("", 20, 37, 230, 20, this, GlobalStyle.textFormatOrange);
            addChild(new GImageBitmap("equipment_successRate.png", 70, 55));
            var loc1=new Object /* flash.text.TextFormat */("", 20, 65280);
            var loc2=new GTextFormat("", 12, 15788607, null, null, null, null, null, flash.text.TextFormatAlign.CENTER);
            this._maxRootTxt = ObjCreate.createTextField("", 0, 3, 274, 20, this, loc2);
            this._rateTxt = ObjCreate.createTextField("0%", 160, 55, 100, 40, this, loc1);
            this._breakBtn = ObjCreate.createGButton(Language.getString(30221), 100, 230, 80, 23, this);
            this._breakBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBreakBtnClickHandler);
            var loc5=0;
            while (loc5 < this._pointArr.length) 
            {
                loc3 = new PropItem();
                loc3.x = this._pointArr[loc5].x;
                loc3.y = this._pointArr[loc5].y;
                loc3.visible = false;
                loc3.setSize(33, 33);
                loc3.isDragAble = false;
                loc3.mouseChildren = false;
                loc3.addEventListener(flash.events.MouseEvent.CLICK, this.onPropItemClickHandler);
                addChild(loc3);
                this._propItemArr.push(loc3);
                (loc4 = new EquipPropButton(Language.getString(30222))).x = this._pointArr[loc5].x;
                loc4.y = this._pointArr[loc5].y;
                loc4.useHandCursor = true;
                addChild(loc4);
                loc4.addEventListener(flash.events.MouseEvent.CLICK, this.onLuckPropClickHandler);
                this._luckPropArr.push(loc4);
                ++loc5;
            }
            this._firstPropItem = this._propItemArr[0] as PropItem;
            this._firstPropItem.visible = true;
            this._firstPropItem.removeEventListener(flash.events.MouseEvent.CLICK, this.onPropItemClickHandler);
            (this._luckPropArr[0] as EquipPropButton).visible = false;
            var loc6=Language.getString(30223);
            var loc7;
            (loc7 = new AcupointRecommendPanel()).setText(loc6);
            loc7.x = -5;
            loc7.y = 265;
            addChild(loc7);
            this._rootBreakItemInfo = ItemConfig.instance.getItemByType(ECategory._ECategoryProp, EProp._EPropRootBreakThrough);
            if (this._rootBreakItemInfo && this._rootBreakItemInfo.item) 
            {
                this._firstPropItem.itemData = new ItemData(this._rootBreakItemInfo.item.code);
            }
            this._firstPropItem.filters = [];
            this.updateByStep(1);
            return;
        }

        /* internal  */onBreakBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc6=null;
            var loc7=0;
            var loc8=0;
            var loc10=0;
            var loc1=AcupointData.getRootLevel(this._step);
            var loc2=AcupointData.getRootBreakThroughsLevelByStep(this._step);
            if (!(loc1 == 120) && !AcupointConfig.instance.isCanBreakThrough(loc1)) 
            {
                Alert.show(Language.getString(30224), null, Alert.OK, null);
                return;
            }
            if (!AcupointConfig.instance.isCanBreakThrough(loc1) || !(loc1 == loc2)) 
            {
                MsgManager.showRollTipsMsg(Language.getString(30228));
                return;
            }
            if (this._propRootBreakNum < this._perNum && AcupointConfig.instance.isCanBreakThrough(loc1) && AcupointData.getRootBreakThroughsLevelByStep(this._step) == loc1) 
            {
                QuickBuyWindow.instance.updateWindow(ECategory._ECategoryProp, EProp._EPropRootBreakThrough);
                QuickBuyWindow.instance.updateDefaultAmount(this._perNum);
                QuickBuyWindow.instance.show();
                return;
            }
            var loc3=new Map<any, any> /* flash.utils.Dictionary */();
            var loc4;
            (loc4 = Cache.instance.pack.backPackCache.getTypeItems(ECategory._ECategoryProp, EProp._EPropRootBreakThrough)).sortOn(["bind"], Array.DESCENDING | Array.NUMERIC);
            var loc5=this._rateNum * this._perNum;
            var loc9=0;
            while (loc9 < loc4.length) 
            {
                if ((loc7 = (loc6 = loc4[loc9] as ItemData).itemAmount) <= loc5 - loc8) 
                {
                    loc10 = loc7;
                }
                else 
                {
                    loc10 = loc5 - loc8;
                }
                loc3[loc6.uid] = loc10;
                if ((loc8 = loc8 + loc10) >= loc5) 
                {
                    break;
                }
                ++loc9;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.AcupointRootBreakThrough, {"step": _step, "props": loc3}));
            return;
        }

        /* internal  */onPropItemClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.target as PropItem;
            var loc2=this._propItemArr.indexOf(loc1);
            loc1.itemData = null;
            loc1.amount = 0;
            loc1.visible = false;
            var loc3;
            (loc3 = this._luckPropArr[loc2] as EquipPropButton).visible = true;
            --this._rateNum;
            this._rateTxt.text = this._perRate * this._rateNum + "%";
            return;
        }

        /* internal  */onLuckPropClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc6=null;
            var loc1=arg1.target as EquipPropButton;
            if (typeof arg1.target === "equippropbutton") 
            {
                loc1 = arg1.target as EquipPropButton;
            }
            else 
            {
                arg1.currentTarget is EquipPropButton;
            }
            loc1 = arg1.currentTarget as EquipPropButton;
            if (loc1 == null) 
            {
                return;
            }
            var loc2=AcupointData.getRootLevel(this._step);
            var loc3=AcupointData.getRootBreakThroughsLevelByStep(this._step);
            if (!AcupointConfig.instance.isCanBreakThrough(loc2) || !(loc2 == loc3)) 
            {
                MsgManager.showRollTipsMsg(Language.getString(30228));
                return;
            }
            var loc4=this._luckPropArr.indexOf(loc1);
            var loc5;
            if ((loc5 = (this._rateNum + 1) * this._perNum) <= this._propRootBreakNum) 
            {
                loc1.visible = false;
                (loc6 = this._propItemArr[loc4] as PropItem).visible = true;
                if (this._rootBreakItemInfo && this._rootBreakItemInfo.item) 
                {
                    loc6.itemData = new ItemData(this._rootBreakItemInfo.item.code);
                }
                loc6.setAmountText(this._perNum);
                this._rateNum = this._rateNum + 1;
                this._rateTxt.text = this._perRate * this._rateNum + "%";
            }
            else 
            {
                this._luckPropIdx = loc4;
                QuickBuyWindow.instance.updateWindow(ECategory._ECategoryProp, EProp._EPropRootBreakThrough);
                QuickBuyWindow.instance.updateDefaultAmount(this._perNum);
                QuickBuyWindow.instance.show();
                QuickBuyWindow.instance.addEventListener(EventName.QuickBuyCancel, this.onQuickBuyCancelHandler);
            }
            return;
        }

        /* internal  */onQuickBuyCancelHandler(arg1: DataEvent): void
        {
            this._luckPropIdx = -1;
            return;
        }

        /* internal  */setLuckPropEnable(arg1: Boolean): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=1;
            while (loc3 < this._pointArr.length) 
            {
                loc2 = this._luckPropArr[loc3] as EquipPropButton;
                loc2.mouseEnabled = arg1;
                loc2.mouseChildren = arg1;
                loc2.visible = true;
                if (!arg1) 
                {
                    loc1 = this._propItemArr[loc3] as PropItem;
                    loc1.visible = false;
                    loc1.itemData = null;
                    loc1.amount = 0;
                }
                ++loc3;
            }
            return;
        }

        public updateByStep(arg1: int): void
        {
            this._step = arg1;
            var loc1=AcupointData.getRootBreakThroughsLevelByStep(arg1);
            var loc2=AcupointConfig.instance.getRootBreakThroughByLevel(loc1);
            if (loc2) 
            {
                this._maxRootTxt.htmlText = Language.getStringByParam(30225, loc2.maxRoot);
                this._perNum = loc2.tAcupointConfig.num;
                this._perRate = loc2.tAcupointConfig.rate;
                this._rateTxt.text = this._perRate * this._rateNum + "%";
                this.updateProp(true);
                this.setLuckPropEnable(true);
            }
            else 
            {
                this._maxRootTxt.htmlText = Language.getString(30226);
                this._rateTxt.text = "";
                this.setLuckPropEnable(false);
            }
            this.updateRootAddInfo(arg1);
            return;
        }

        /* internal  */updateRootAddInfo(arg1: int): void
        {
            var loc10=0;
            var loc11=0;
            var loc1=AcupointData.getAttributesByStep(arg1, 1, AcupointData.acupointInfo.attributes);
            var loc2=AcupointData.getAttributesByStep(arg1, 3, AcupointData.acupointInfo.attributes);
            var loc3=Cache.instance.role.careerBasic;
            var loc4=Cache.instance.role.careerEnabled;
            var loc5=AcupointData.getAttributeStr(loc1, "", loc3, loc4, true, false);
            var loc6=AcupointData.getRootLevel(arg1);
            this._rootTxt.text = Language.getStringByParam(30338, loc6) + loc5;
            var loc7;
            var loc8=(loc7 = AcupointData.acupointInfo.rootEffects)[arg1];
            var loc9;
            if (loc9 = loc1[0] as SAttributeUpdate) 
            {
                loc11 = (loc10 = loc9.value) / (loc8 + 100) * 100;
                this._addDesTxt.text = Language.getString(30257) + loc11 + "\t" + Language.getString(30258) + (loc10 - loc11);
            }
            return;
        }

        public updateProp(arg1: Boolean=false): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            var loc4=0;
            var loc5=0;
            this._propRootBreakNum = Cache.instance.pack.backPackCache.getItemCountByCategoryAndType(ECategory._ECategoryProp, EProp._EPropRootBreakThrough);
            if (arg1 || this._propRootBreakNum < this._perNum) 
            {
                loc3 = 1;
                while (loc3 < this._pointArr.length) 
                {
                    loc1 = this._propItemArr[loc3] as PropItem;
                    loc1.visible = false;
                    loc1.itemData = null;
                    loc1.amount = 0;
                    loc2 = this._luckPropArr[loc3] as EquipPropButton;
                    loc2.visible = true;
                    ++loc3;
                }
                this._rateNum = 1;
                this._rateTxt.text = this._perRate * this._rateNum + "%";
            }
            if (this._propRootBreakNum >= this._perNum) 
            {
                this._firstPropItem.filters = [];
                this._firstPropItem.setAmountText(this._perNum);
                if (this._propRootBreakNum - this._perNum * this._rateNum >= this._perNum && !(this._luckPropIdx == -1)) 
                {
                    loc2 = this._luckPropArr[this._luckPropIdx] as EquipPropButton;
                    loc2.visible = false;
                    loc1 = this._propItemArr[this._luckPropIdx] as PropItem;
                    loc1.visible = true;
                    if (this._rootBreakItemInfo && this._rootBreakItemInfo.item) 
                    {
                        loc1.itemData = new ItemData(this._rootBreakItemInfo.item.code);
                    }
                    loc1.setAmountText(this._perNum);
                    this._rateNum = this._rateNum + 1;
                    this._luckPropIdx = -1;
                }
                else if (this._propRootBreakNum < this._perNum * this._rateNum) 
                {
                    loc4 = Math.ceil((this._perNum * this._rateNum - this._propRootBreakNum) / this._perNum);
                    loc5 = 0;
                    loc3 = 1;
                    while (loc3 < this._pointArr.length) 
                    {
                        loc1 = this._propItemArr[loc3] as PropItem;
                        if (loc1.visible) 
                        {
                            loc1.visible = false;
                            loc1.itemData = null;
                            loc1.amount = 0;
                            loc2 = this._luckPropArr[loc3] as EquipPropButton;
                            loc2.visible = true;
                            loc5 = loc5 + 1;
                            --this._rateNum;
                        }
                        if (!(loc5 >= loc4)) 
                        {
                        };
                        ++loc3;
                    }
                }
                this._rateTxt.text = this._perRate * this._rateNum + "%";
            }
            else 
            {
                this._firstPropItem.filters = [FilterConst.colorFilter];
                this._firstPropItem.setAmountText(this._perNum);
            }
            return;
        }

        /* internal */ /* var */_maxRootTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_rateTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_breakBtn: GButton;

        /* internal */ /* var */_step: int;

        /* internal */ /* var */_pointArr: Array<any>;

        /* internal */ /* var */_propItemArr: Array<any>;

        /* internal */ /* var */_luckPropArr: Array<any>;

        /* internal */ /* var */_perNum: int;

        /* internal */ /* var */_perRate: int;

        /* internal */ /* var */_propRootBreakNum: int;

        /* internal */ /* var */_rootBreakItemInfo: ItemInfo;

        /* internal */ /* var */_firstPropItem: PropItem;

        /* internal */ /* var */_rateNum: int=1;

        /* internal */ /* var */_luckPropIdx: int=-1;

        /* internal */ /* var */_rootTxt: GTextFiled;

        /* internal */ /* var */_addDesTxt: GTextFiled;
    }
